package com.ouyeel.wlxer.util;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.base.TypeItem;

import java.util.List;

/**
 * Created by rankaifeng on 2017/11/17.
 */

public class MyPopupWindow extends PopupWindow {
    private PopupWindow mPopupWindow;
    private View customView;
    ListViewOnitemClick listViewOnitemClick;

    public void setListViewOnitemClick(ListViewOnitemClick listViewOnitemClick) {
        this.listViewOnitemClick = listViewOnitemClick;
    }

    public interface ListViewOnitemClick {
        void onItemClickListener(View view, int position);
    }

    /**
     * popupwindow显示
     */
    public void createPopupWindow(int resource, final Activity activity,
                                  final List<TypeItem> typeItemList) {
        customView = LayoutInflater.from(activity).inflate(resource, null);
        mPopupWindow = new PopupWindow(customView, ActionBar.LayoutParams.FILL_PARENT,
                ActionBar.LayoutParams.MATCH_PARENT);
        ListView mypopuList = customView.findViewById(R.id.mypopu_list);
        LinearLayout linMypopu = customView.findViewById(R.id.lin_mypopu);
        TypeItemAdapter adapter = new TypeItemAdapter(activity, typeItemList);
        mypopuList.setAdapter(adapter);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        // 刷新状态（必须刷新否则无效）
        mPopupWindow.update();
        mypopuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mPopupWindow.dismiss();
                listViewOnitemClick.onItemClickListener(view, i);
            }
        });
        linMypopu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mPopupWindow.dismiss();
            }
        });
    }


    public void showAsDropDowns(View view, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT >= 24) {
            int[] location = new int[2];
            view.getLocationOnScreen(location);
            // 7.1 版本处理
            if (Build.VERSION.SDK_INT == 25) {
                //【note!】Gets the screen height without the virtual key
                WindowManager wm = (WindowManager) mPopupWindow.getContentView().getContext()
                        .getSystemService(Context.WINDOW_SERVICE);
                int screenHeight = wm.getDefaultDisplay().getHeight();
                mPopupWindow.setHeight(screenHeight - location[1] - view.getHeight() - yoff);
            }
            mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, xoff, location[1]
                    + view.getHeight() + yoff);
        } else {
            mPopupWindow.showAsDropDown(view);
        }
    }

    public void pWindowDisOrShow(Activity activity, List<TypeItem> typeItems, View view) {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        } else {
            createPopupWindow(R.layout.mypopuwindow, activity, typeItems);
            showAsDropDowns(view, 0, 0);
        }
    }

    class TypeItemAdapter extends BaseAdapter {
        Context mContext;
        List<TypeItem> typeItemList;

        public TypeItemAdapter(Context mContext, List<TypeItem> typeItemList) {
            this.mContext = mContext;
            this.typeItemList = typeItemList;
        }

        @Override
        public int getCount() {
            return typeItemList.size();
        }

        @Override
        public Object getItem(int position) {
            return typeItemList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(mContext).inflate(R.layout.mypopu_item, null);
            TextView tvMypopu = view.findViewById(R.id.tv_mypopu);
            tvMypopu.setText(typeItemList.get(position).getTitle());
            return view;
        }
    }
}
